package com.example.demo.config;/**
 * ClassName: CorsConfig
 * Package: com.example.demo.config
 * Description:
 *
 * @Author 理理
 * @Create 2024/11/23 23:05
 * @Version 1.0
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 *
 *@title: CorsConfig
 *@Author: Tangerine
 *@Date: 2024/11/23 23:05
 *@version 1.0
 */
//跨域设置
@Configuration
public class CorsConfig {
    private static final long MAX_AGE = 24*60*60;

    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");//设置访问源地址
        corsConfiguration.addAllowedHeader("*");//设置访问源请求头
        corsConfiguration.addAllowedMethod("*");//设置访问源请求方法
        corsConfiguration.setMaxAge(MAX_AGE);
        source.registerCorsConfiguration("/**",corsConfiguration);//对接口配置跨域设置
        return new CorsFilter(source);
    }
}
